<template>
	<view>
		<view class="main text-lg">
			<view class="indexContents">
				<view class="list p30">
					<view v-if="addressSet.type==1">收货人 :</view>
					<view v-if="addressSet.type==0">发货人 :</view>
					<view><input maxlength="8" v-model="addressSet.name" type="text" placeholder="姓名" /></view>
				</view>
				<view class="list p30">
					<view>手机号 :</view>
					<view><input maxlength="11" v-model="addressSet.mobile" type="number" placeholder="手机号" /></view>
				</view>
				<view class="list list3  p30 ">
					<view>地址 :</view>
					<view @tap="addressTab()">{{addressSet.address}}</view>
					<image class="go" src="../../static/image/go.png" mode=""></image>
				</view>
				<view class="list list2  p30 ">
					<view>详细地址 :</view>
					<view style="padding-top: 30rpx;">
						<textarea maxlength="60" v-model="addressSet.doorplate" placeholder="详细地址，例如：3号楼2单元105" /></view>
				</view>
			</view> 
			<view   style="margin-top: 24rpx; " class="indexContents "> 
				<view class="list " style="display: block;  border: none!important;padding-left: 30rpx;">
					设为默认地址:
					<view class="fr" style="text-align: right;margin: 0;">
						<switch style="transform:scale(0.7) " color="#1BBF80!important" :checked="addressSet.isDefault" @change="switch1Change" /></view>
				</view>
			</view>	
			<view v-if="addressType == 1" @tap="removes()" style="margin-top: 24rpx; border: none!important;" class="list p30 c2">
				删除收货地址
			</view>
		</view>
		<u-button   @tap="send()" class="addBtn" type="success">{{submitMessage}}</u-button>
		<models  @success="Delete" msg="确定删除地址" title='提示' ref="models"></models>
	</view>
</template>

<script> 
	import {addressAdd,addressupdate,addressDelete} from '@/api/api.js'
export default { 
	data() {
		return { 
			submitMessage:"保存",
			addressType:0, 
			addressSet:{
				name: '',
				mobile: '',
				address: '选择地址',
				doorplate: '',
				isDefault: true, //是否默认 
				longitude:'',//精度
				latitude:'', 
				type:1
			}
		}
	}, 
	onLoad(e) {
		this.addressSet.type = e.type  
	},
	onShow() {
		let locationsLtLg = uni.getStorageSync('locationsLtLg') 
		this.addressSet.longitude = locationsLtLg.longitude
		this.addressSet.latitude = locationsLtLg.latitude
		
		let locationsTxt = uni.getStorageSync('locationsTxt')
		if(locationsTxt != ''){
			this.addressSet.address = locationsTxt
		}
		
		let addressSet = uni.getStorageSync('addressSet')
		if(addressSet != ''){
			this.addressType = 1
			this.addressSet = addressSet
		}
		let title = ''
		if(this.addressSet.type ==1){ 
				title='收货地址' 
		}else{ 
				title='发货地址' 
		}
		uni.setNavigationBarTitle({
			title:title
		})
		uni.removeStorageSync('addressSet')
	},
	onUnload() {
		uni.removeStorageSync('locationsLtLg')
		uni.removeStorageSync('locationsTxt')
	},
	methods: { 
		async Delete(){
			let data = await addressDelete({
				id:this.addressSet.id
			})
			if (data.code == 0) { 
				this.$backs()
			}
		},
		removes(){
			this.$refs['models'].show()
		},
		addressTab(){
			uni.navigateTo({
				url:'../mapSet/mapSet?state=1'
			}) 
		},
		send() {
			try{
				if (this.addressSet.name == '') throw '姓名不能为空'
				if (this.addressSet.name.length == 1) throw '姓名长度不正确'
				if (!/^1[3456789]\d{9}$/.test(this.addressSet.mobile)) throw '手机号不正确'
				if (this.addressSet.address == '选择地址') throw '收货地址不能为空'
				if (this.addressSet.doorplate.length == '') throw '门牌号不能为空'
			}catch(e){
				this.toast(e)
				return
			}    
			this.success()
		},
		success(){ 
			this.addressSet.doorplate = this.addressSet.doorplate.replace(/\n/g,'') 
			if(this.addressType == 0){ 
				addressAdd(this.addressSet).then(res=>{
					if(res.code == 0){
						this.$backs()
					} 
				})		 
					 
			}else{
				addressupdate(this.addressSet).then(res=>{
					if(res.code == 0){
						this.$backs()
					} 
				})	
			} 
		},
		switch1Change(e) {
			this.addressSet.isDefault = e.detail.value
		},
		toast(txt) {
			uni.showToast({
				title: txt,
				icon: 'none',
				mask: true,
				duration: 800
			});
		}
	}
};
</script>

<style lang="less" scoped>
@import './index.less';
</style>
